package main

import (
	httptransport "github.com/go-kit/kit/transport/http"
	"github.com/gorilla/mux"
	"net/http"
	"ruoyi-micro/Services"
)

func main() {
	//mux := mux.NewRouter()
	//mux.HandleFunc("/user/add", Services.UserService.Add)
	//svc, err := client.New("http://localhost:8081", map[string][]http.ClientOption{})
	//if err != nil {
	//	panic(err)
	//}
	//
	//r, err := svc.Foo(context.Background(), "hello")
	//if err != nil {
	//	fmt.Println("Error:", err)
	//	return
	//}
	//fmt.Println("Result:", r)

	//http.ListenAndServe(":8089", mux)

	// 优雅关闭
	//shutdown.GracefullyShutdown(srv)

	user := Services.UserService{}
	endp := Services.GenUserEndPoint(user)

	serverHandler := httptransport.NewServer(endp, Services.DecodeUserRequest, Services.EncodeUserResponse) //使用go kit创建server传入我们之前定义的两个解析函数

	r := mux.NewRouter() //使用mux来使服务支持路由
	//r.Handle(`/user/{uid:\d+}`, serverHandler) //这种写法支持多种请求方法，访问Examp: http://localhost:8080/user/121便可以访问
	r.Methods("GET", "DELETE").Path(`/user/{uid:\d+}`).Handler(serverHandler) //这种写法限定了请求只支持GET方法

	// 心跳监听接口
	r.Methods("GET").Path("/health").HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
		writer.Header().Set("Content-type", "application/json")
		writer.Write([]byte(`{"status":"ok"}`))
	})
	// 端口8080启动监听
	http.ListenAndServe(":8080", r)
}
